package sword;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 海琳琦
 * @Date: 2022/5/26 21:37
 * https://leetcode.cn/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/
 */
public class Title39 {

    public int majorityElement(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        int key = nums[0];
        int max = 1;
        for (int i = 0; i < nums.length; i++) {
            int value = map.getOrDefault(nums[i], 0) + 1;
            map.put(nums[i], value);
            if (value > max) {
                key = nums[i];
                max = value;
            }
        }
        return key;
    }

    public static void main(String[] args) {

    }
}
